package com.example.ams.service;

import com.example.ams.dto.EmployeeDTO;
import com.example.ams.entity.Employee;
import com.example.ams.repository.EmployeeRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工服务类
 * Employee Service
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    /**
     * 查询所有员工
     */
    public List<EmployeeDTO> findAll() {
        return employeeRepository.findAll().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 根据ID查询员工
     */
    public EmployeeDTO findById(Long id) {
        Employee employee = employeeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("员工不存在，ID: " + id));
        return convertToDTO(employee);
    }

    /**
     * 根据工号查询员工
     */
    public EmployeeDTO findByEmpNo(String empNo) {
        Employee employee = employeeRepository.findByEmpNo(empNo)
                .orElseThrow(() -> new RuntimeException("员工不存在，工号: " + empNo));
        return convertToDTO(employee);
    }

    /**
     * 创建员工
     */
    @Transactional
    public EmployeeDTO create(EmployeeDTO employeeDTO) {
        // 检查工号是否已存在
        if (employeeRepository.existsByEmpNo(employeeDTO.getEmpNo())) {
            throw new RuntimeException("工号已存在: " + employeeDTO.getEmpNo());
        }

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee, "id", "createdAt");
        
        // 默认在职状态
        if (employee.getStatus() == null) {
            employee.setStatus(1);
        }

        Employee saved = employeeRepository.save(employee);
        return convertToDTO(saved);
    }

    /**
     * 更新员工
     */
    @Transactional
    public EmployeeDTO update(Long id, EmployeeDTO employeeDTO) {
        Employee employee = employeeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("员工不存在，ID: " + id));

        // 如果修改了工号，检查新工号是否已被其他员工使用
        if (!employee.getEmpNo().equals(employeeDTO.getEmpNo())) {
            if (employeeRepository.existsByEmpNo(employeeDTO.getEmpNo())) {
                throw new RuntimeException("工号已存在: " + employeeDTO.getEmpNo());
            }
        }

        BeanUtils.copyProperties(employeeDTO, employee, "id", "createdAt");
        Employee updated = employeeRepository.save(employee);
        return convertToDTO(updated);
    }

    /**
     * 删除员工
     */
    @Transactional
    public void delete(Long id) {
        if (!employeeRepository.existsById(id)) {
            throw new RuntimeException("员工不存在，ID: " + id);
        }
        employeeRepository.deleteById(id);
    }

    /**
     * 根据部门查询员工
     */
    public List<EmployeeDTO> findByDept(String dept) {
        return employeeRepository.findByDept(dept).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 根据在职状态查询员工
     */
    public List<EmployeeDTO> findByStatus(Integer status) {
        return employeeRepository.findByStatus(status).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * Entity转DTO
     */
    private EmployeeDTO convertToDTO(Employee employee) {
        EmployeeDTO dto = new EmployeeDTO();
        BeanUtils.copyProperties(employee, dto);
        return dto;
    }
}

